package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.core.constants.HttpStatus;
import com.demo.core.exception.BusinessException;
import com.demo.core.exception.ParameterException;
import com.demo.dao.TeamCompositionDao;
import com.demo.dao.TeamProfileDao;
import com.demo.dao.UserDao;
import com.demo.domain.TeamComposition;
import com.demo.domain.User;
import com.demo.service.TeamCompositionService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TeamCompositionServiceImpl extends ServiceImpl<TeamCompositionDao, TeamComposition> implements TeamCompositionService {

    @Autowired
    TeamCompositionDao teamCompositionDao;

    @Autowired
    TeamProfileDao teamProfileDao;

    @Autowired
    UserDao userDao;

    /*CRUD*/
    @Override
    public TeamComposition getByTcId(Long tcId) throws ParameterException{
        if(tcId == null){
            throw new ParameterException("null");
        }
        TeamComposition teamComposition = teamCompositionDao.selectById(tcId);
        if(teamComposition.getDelFlag() == 1){
            return null;
        }
        return teamComposition;
    }

    @Override
    public Integer insertTeamComposition(TeamComposition teamComposition) throws ParameterException {
        int row = 0;
        if(teamComposition.getUid()!=null && teamComposition.getUrealname()!=null && teamComposition.getTeamId()!=null && teamComposition.getMemberType()!= null){
            teamComposition.setDelFlag(0);
            row = teamCompositionDao.insert(teamComposition);
        }else{
            return null;
        }
        return row;
    }

    @Override
    public Integer updateTeamComposition(TeamComposition teamComposition) {
        int row = 0;
        if(teamComposition.getId()!=null && teamComposition.getUid()!=null && teamComposition.getUrealname()!=null && teamComposition.getTeamId()!=null && teamComposition.getMemberType()!= null && teamComposition.getDelFlag() == 0){
            row = teamCompositionDao.updateById(teamComposition);
        }else{
            return null;
        }
        return row;
    }

    @Override
    public Integer deleteTeamCompositionById(TeamComposition teamComposition) throws ParameterException {
        int row = 0;
        if(teamComposition.getId()!=null){
            row = teamCompositionDao.deleteTeamCompositionById(teamComposition);
        }

        return row;
    }

    @Override
    public List<TeamComposition> selectTeamCompositionList(TeamComposition teamComposition) throws ParameterException {
        List<TeamComposition> teamCompositionList = new ArrayList<>();
        if (teamComposition.getUid() != null || teamComposition.getUrealname()!=null || teamComposition.getTeamId()!=null) {
            teamCompositionList=teamCompositionDao.selectTeamCompositionList(teamComposition.getUid(),teamComposition.getUrealname(),teamComposition.getTeamId());
        }
        return teamCompositionList;
    }

    @Override
    public List<TeamComposition> listAllTeamComposition(){
        List<TeamComposition> teamCompositionList = teamCompositionDao.listAllTeamComposition();
        return teamCompositionList;
    }

    //得到某队伍的所有成员
    @Override
    public List<TeamComposition> getTeamMember(TeamComposition teamComposition) throws ParameterException {
        if (teamComposition == null) {
            throw new ParameterException("teamComposition为null");
        }
        if (teamComposition.getTeamId() == null || teamComposition.getTeamId() <= 0L) {
            throw new ParameterException("teamComposition.teamId为null或者值非法");
        }
        LambdaQueryWrapper<TeamComposition> lqw = new LambdaQueryWrapper<TeamComposition>();
//        根据id查找对应队伍的组成成员
        if (teamComposition.getTeamId() != null) {
            lqw.eq(TeamComposition::getTeamId, teamComposition.getTeamId());
            lqw.orderByAsc(TeamComposition::getMemberType);
        }
        List<TeamComposition> teamCompositions = teamCompositionDao.selectList(lqw);
        return teamCompositions;
    }

//  验证申请报名信息的用户是否已存在于正在报名的队伍中
    @Override
    public boolean getUserInfo(Long teamId,
                         Long userId) throws ParameterException {
        if(teamId == null && userId == null && teamId <= 0l && userId == 0l){
            throw new ParameterException("未正确传入参数");
        }
        boolean user =
                teamCompositionDao.getUserInfo(userId, teamId);
        System.out.println("user是什么？"+user);
        return user;
    }

    @Override
    public boolean deleTeamMember(Long teamId, String urealname, Long memberId, Long uid) throws ParameterException {
        if (teamId == null || teamId == 0l || urealname.isEmpty() || uid == 0l || uid == null) {
            throw new ParameterException("未正确传入参数");
        }
        if (teamId == null || teamId <= 0L) {
            throw new ParameterException("teamId为null或者值非法");
        }
        int uType = teamCompositionDao.getUserType(uid, teamId);
        if (uType < 0) {
            throw new ParameterException("uType值非法");
        }
        int row = 0;
//        为队长或指导老师才有权限
        if (uType == 0 || uType == 1) {
            if (teamId != null && !urealname.isEmpty()) {
//            存储在teamComposition表中删除的记录数
                row = teamCompositionDao.deleteTeamMember(teamId, urealname, memberId);
//            存储对应队伍信息中删除的成员数
                int row1 = teamProfileDao.decrementTeamMember(teamId);
                if (row1 > 0) {
                    System.out.println("删除一名队员");
                } else {
                    System.out.println("删除失败");
                    return false;
                }
            }
        } else {
            throw new BusinessException(HttpStatus.CONFLICT,"当前账户无权限删除队员");
        }
        return row > 0;
    }

    @Override
    public boolean increTeamMember(TeamComposition teamComposition, Long uid) throws ParameterException {
        Long teamId = teamComposition.getTeamId();
        Long memberId = teamComposition.getUid();
        String urealname = teamComposition.getUrealname();
        boolean isFound = false;
        int lackMember = teamProfileDao.getLackNumber(teamId);
        int uType = teamCompositionDao.getUserType(uid, teamId);
        if (uType < 0) {
            throw new ParameterException("uType值非法");
        }
        int row = 0;
//        为队长或指导老师才有权限
        if (uType == 0 || uType == 1) {
            if (teamId != null && !urealname.isEmpty() && teamId > 0l && memberId != null && memberId > 0l) {
                if (lackMember > 0) {
                    User member = userDao.selectById(memberId);
//                如果能在user表中找到该学号对应的成员
                    if (member != null) {
//                    验证学号和真实姓名是否对应
                        isFound = member.getURealname().equals(urealname);
//                    如果不一致，则抛出错误
                        if (!isFound) {
                            throw new BusinessException(HttpStatus.CONFLICT, "学号和姓名不一致");
                        }
                    }
//            在teamComposition表中插入成员
                    row = teamCompositionDao.insert(teamComposition);
                } else {
                    throw new BusinessException(HttpStatus.CONFLICT, "队伍已满人");
                }
            } else {
                throw new ParameterException("队伍编号或真实姓名或新增队员的ID为空或不符合格式");
            }
        } else {
            throw new RuntimeException("当前账户无权限删除队员");
        }
        return row > 0;
    }
}
